﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BUS;
using DTO;

namespace ASP.ADO
{
    public partial class EditDaiLy : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.IsPostBack == false)
            {
                string maDaiLy = Request.QueryString["madaily"];
                txtMaDaiLy.Text = maDaiLy;


                ddlLoaiDaiLy.DataValueField = "MaLoaiDaiLy";
                ddlLoaiDaiLy.DataTextField = "TenLoaiDaiLy";
                ddlLoaiDaiLy.DataSource = LoaiDaiLyBUS.SellectLoaiDaiLyAll();
                ddlLoaiDaiLy.DataBind();

                ddlMaQuan.DataValueField = "MaQuan";
                ddlMaQuan.DataTextField = "TenQuan";
                ddlMaQuan.DataSource = QuanBUS.SelectQuanAll();
                ddlMaQuan.DataBind();
                // load du lieu len san


                List<HoSoDaiLyDTO> tan = new List<HoSoDaiLyDTO>();
                tan = HoSoDaiLyBUS.SelectHoSOByID(maDaiLy);
                if (tan.Count == 0)
                {
                    lblResult.Text = "Mã Đại Lý Không hợp Lệ!";
                    return;
                }


                this.txtDiaChi.Text = tan[0].DiaChi.ToString();
                this.txtEmail.Text = tan[0].EMail;

                //this..Text = dgvHoSoDaiLy.CurrentRow.Cells["MaHoSoDaiLy"].Value.ToString();
                this.txtTenDaiLy.Text = tan[0].TenDaiLy.ToString();
                this.txtTienNo.Text = tan[0].TienNo.ToString();
                this.txtDienThoai.Text = tan[0].DienThoai.ToString();

                this.ddlLoaiDaiLy.SelectedValue = tan[0].MaLoaiDaiLy.ToString();
                this.ddlMaQuan.SelectedValue = tan[0].MaQuan.ToString();

                this.txtNgayTiepNhan.Text = tan[0].NgayTiepNhan.ToShortDateString();



            }
        }

        protected void btnAddDaiLy_Click(object sender, EventArgs e)
        {


            HoSoDaiLyDTO hoSoDaiLyDTO = new HoSoDaiLyDTO();
            hoSoDaiLyDTO.TenDaiLy = txtTenDaiLy.Text;
            hoSoDaiLyDTO.DiaChi = txtDiaChi.Text;
            hoSoDaiLyDTO.DienThoai = txtDienThoai.Text;
            hoSoDaiLyDTO.EMail = txtEmail.Text;
            hoSoDaiLyDTO.MaHoSoDaiLy = txtMaDaiLy.Text;
            hoSoDaiLyDTO.MaLoaiDaiLy = ddlLoaiDaiLy.SelectedValue.ToString();
            hoSoDaiLyDTO.MaQuan = ddlMaQuan.SelectedValue.ToString();
            hoSoDaiLyDTO.NgayTiepNhan = DateTime.Parse(txtNgayTiepNhan.Text);
            hoSoDaiLyDTO.TienNo = float.Parse(txtTienNo.Text);


            bool isSucessful = HoSoDaiLyBUS.UpdateHoSo(hoSoDaiLyDTO);


            if (isSucessful == true)
            {
                lblResult.Text = "Sửa thành công!";
            }
            else
                lblResult.Text = "Sửa thất bại!";

           
        }

        protected void btnXoaDaiLy_Click(object sender, EventArgs e)
        {
            HoSoDaiLyDTO hoSo = new HoSoDaiLyDTO();
            hoSo.MaHoSoDaiLy = txtMaDaiLy.Text;
            bool isSucessful = HoSoDaiLyBUS.DeleteHoSoDaiLy(hoSo);

            if (isSucessful == true)
            {
                lblResult.Text = "Xóa Thành Công!";
            }
            else
                lblResult.Text = "Xóa Thất Bại!";
            
        }
    }
}